class JnlpController < ApplicationController
  
  def make
    
    sfilename=Time.now.to_f.to_s+rand(0xFFFFFFFF).to_s+".jnlp"
    
    if(params[:port].blank?)
      params[:port]="443"
    end
    if($cache.nil?)then
      @content=File.open("public/kerjodando.jnlp"){|f|f.read}
      logger.warn("the message should be show once in around-running")
      @content.gsub!('$$title','dargens')
      $cache=@content
    end
    #  str=File.dirname(request.protocol << request.host_with_port << (request.path.split('?',2)[0]));
    str=request.protocol << request.host_with_port 
    @content=$cache.gsub('$$codebase',str+"/Dev-test/")
    puts str
    # @content.gsub!('$$href',(str << '/Dev-test/kerjodando.jnlp'))
    #@content.gsub!('$$href',request.request_uri.split('/',3)[2])
    #removed by casper to solve a problem caused by jre1.60_02 or 03
    @content.gsub!('$$href','')
    
    url=params[:server].split('/',5)[3]
    
    s=Swarm.find(:first,:conditions=>["url = :url ",{:url=>url}])
    @content.gsub!('$$icon',('images/kerjodando_pic/' << s.pic_url))
    
    replace_str = getXmlproperty('user_public_ip',request.remote_ip)
    
    puts request.remote_ip
    logger.warn("request ip=>"+request.remote_ip)
    request.parameters.each {|key,value|replace_str<<getXmlproperty(key,value)}
  request.parameters.each do |key,value|
  end
  

  swarm_url= params[:server].split('//')[1].split('/')[1]
  s=Swarm.find(:first,:conditions=>["url = :url",{:url=>swarm_url}])
  if(s.nil?) then redirect_to :controller=>"index" end

  
  if(session[:user].nil?)        #unlogin yet condition

    ## by casper 07-10-22
    ##uc=UserConnection.find(:first,:conditions=>["user_id = 0 and port=:port and ip =:ip and thetime > :time ",{:ip=>request.remote_ip,:port=>params[:port],:time=>6.minutes.ago}])
    uc=UserConnection.find(:first,:conditions=>["session_id=:session_id and thetime > :time ",{:session_id=>session.session_id.to_s,:time=>6.minutes.ago}])
    reset_session
    if(uc.nil?)
      uc=UserConnection.new(
      :id=>'null',
      :user_id=>session[:user].nil? ? '0' : session[:user][:id],
      :swarm_id=>s.id,
      :ip=>request.remote_ip,
      :port=>params[:port],
      :thetime=>Time.new,
      :session_id=>session.session_id.to_s
      )
      uc.save
    else            
     if(!params[:port].blank?)
    uc.port=params[:port] 
    end     
     
      uc.swarm_id=s.id
      uc.session_id=session.session_id.to_s
      uc.thetime=Time.new
      uc.save
      #flash[:notice]="swarm changed." 
      #redirect_to :back 
      #    return 
    end
    ##replace_str << getXmlproperty("identifier",uc.id.to_s)
    replace_str << getXmlproperty("identifier",session.session_id.to_s)
  else                  #logined condition
    uc_ofsession=UserConnection.find(:all,:conditions=>["user_id=0 and session_id=:session_id and thetime > :time",{:session_id=>session.session_id.to_s,:time=>6.minutes.ago}])
    uc_ofsession.each{|uc3|
      if(!uc3.nil?)then uc3.update_attributes(:user_id=>session[:user].id) end
    }
  
    count=UserConnection.count(:conditions=>["user_id = :user_id and thetime > :thetime",{:user_id=>session[:user].id,:thetime=>6.minutes.ago}])
    if count < 2 then 
    #flash[:notice]="you just can join 2 swarms." 
    #redirect_to :back 
    #return 
    swarm_ids=Array.new
    swarm_ids << s.id
    swarm=Swarm.find(s.id)
    if(swarm.public==2)then swarm_ids+=swarm.contact_info.split('|') end
    
    swarm_ids.each do |id|
     uc=UserConnection.find(:first,:conditions=>["user_id = :user_id  and swarm_id = :swarm_id and thetime > :time",{:user_id=>session[:user].id,:time=>6.minutes.ago,:swarm_id=>id}])
    if(uc.nil?)
      uc=UserConnection.new(
      :id=>'null',
      :user_id=>session[:user].id,
      :swarm_id=>id,
      :ip=>request.remote_ip,
      :port=>params[:port],
      :thetime=>Time.new,      
      :session_id=>session.session_id.to_s
      )
      uc.save
     else
     if(!params[:port].blank?)
     uc.port=params[:port]
     end
      uc.thetime=Time.new
      uc.save
    end
    end
    #those statements are added by casper on 07-10-18
    if count == 1 then 
           flash[:notice]="joined the swarm." 
           redirect_to :back 
          return 
    end    
    end
    #this statement is replaced by casper 07-10-12
   #replace_str << getXmlproperty("identifier",swarm.contact_info) 
    ##replace_str << getXmlproperty("identifier",uc.id.to_s)
    replace_str << getXmlproperty("identifier",session.session_id.to_s)
  end
  @content_type='application/x-java-jnlp'
  @response.headers["Content-Type"] = @content_type
  @response.headers["Cache-Control"]='private,post-check=90,pre-check=100'
  @response.headers["pragma"]='no-cache'
  @response.headers["Expires"]='0'
  @response.headers["Content-Disposition"]='filename='+sfilename

  @content.gsub!('$$properties',replace_str)
   logger.debug("request.remote_ip => "+request.remote_ip+"time=>"+Time.new.to_s+"vincent messege jnlp=>\n"+@content+"\n end vicent messege")

  render :layout=>false
  
  end
  def getXmlproperty(name,value)
  "<property name=\"" + name + "\" value=\"" + value + "\"/>\n";
  end
  
  def download
  end
end
